#ifndef __layer_comm__
#define __layer_comm__
/*
	Communication Layer

	This module defines the Communications

	Use the function hooks to utilise all the advanved features

	This library should be hardware independant
*/
#include "bikirom-ints.h"

//#define NO_CRC

// Easy to use macro's to extract common information
#define	comm_id(x)		x[6]
#define comm_to(x)		x[2]
#define comm_from(x)	x[3]
#define comm_bytes(x)	(unsigned short)x[4] << 8 | x[5]

typedef enum	_eMsgAdr {
	eMsgAtmel	= 0x2A,
	eMsgPc		= 0x1A,
	eMsgEcu		= 0x0A
} _eMsgAdr;

typedef enum	_eMsgCmdResp {
	eCmdRespOK					= 0x01,
	eCmdRespFailed				= 0x02,
	eCmdRespCrcFailed			= 0x03,
	eCmdRespNext				= 0x04,
	eCmdRespSigFailed			= 0x05,
	eCmdRespBufferFull			= 0x06,
	eCmdRespIncomplete			= 0x07,
	eCmdRespUnknownSender		= 0x08,
	eCmdRespUnknownReciever		= 0x09,
	eCmdRespBadHeads			= 0x0A,
	eCmdRespStop				= 0x0B,
	eCmdRespGo					= 0x0C,
	eCmdRespPassed				= 0x0D,
	eCmdRespNotPassed			= 0x0F,
	eCmdRespUnknown				= 0x10,
	eCmdRespTooBig				= 0x11,
	eCmdFwFailed				= 0x12,
	eCmdRespTimeout				= 0x1E,
	eCmdRespFlashWriteTimeout	= 0x1F,
	eCmdRespFlashWriteFailed	= 0x20,
	eCmdRespWatchdogReset		= 0x21,
	eCmdRespSecBadCode			= 0x30,
	eCmdRespSecFunctionLocked	= 0x3A,
	eCmdRespBoot				= 0x60,
	eCmdRespReady				= 0xFF
}_eMsgCmdResp ;

typedef enum	_eMsgCmdRun {
	eCmdRunEraseAll			= 0x80,
	eCmdRunEraseBank1		= 0x81,
	eCmdRunEraseBank2		= 0x82,
	eCmdRunEraseSector		= 0x88,
	eCmdRunSecUnlock		= 0x90,
	eCmdRunSecSetPass		= 0x92,
	eCmdRunSecSetSecLevel	= 0x94,
	eCmdRunGetInfo			= 0x70,
	eCmdRunDiagRxTx			= 0x60,
	eCmdRunDiagRxClk		= 0x61,
	eCmdRunDiagFlash		= 0x62,
	eCmdRunDiagFlash2		= 0x63,
	eCmdRunTestWritePattern = 0x64,
	eCmdRunTestReadPattern	= 0x65,
	eCmdRunGetProgADC		= 0x66,
	eCmdRunGetBoardADC		= 0x67,
	eCmdRunDUTEnable		= 0x68,
	eCmdRunDUTDisable		= 0x69,
	eCmdRunTestOutputs		= 0x6A,
	eCmdRunSetTraceAdr		= 0x6B
}_eMsgCmdRun;

typedef enum	_eMsgId {
	eMsgIdConsultPass		= 0x91,
	eMsgIdConsultIntegrated	= 0x92,
	eMsgIdFlashData			= 0x80,
	eMsgIdEepromData		= 0x82,
	eMsgIdFlashCopy			= 0x84,
	eMsgIdGeneralSpecific	= 0x55,
	eMsgIdRunCommand		= 0xE0,
	eMsgIdLastCmdStatus		= 0xAA,
	eMsgIdBootloader		= 0x70,
	eMsgIdTest				= 0x10
} _eMsgId;

typedef struct _typCommHdr {
	uint8_t	Hdr[2];
	uint8_t	To;
	uint8_t	From;
	uint8_t	Size[2];
	_eMsgId	eMsgId;
} _typCommHdr;

typedef void (*_comm_resp)(uint8_t*, uint16_t);
//typedef void (*_comm_handle)(unsigned char* buffer, unsigned short nLength);

// Initialisation
void	comm_init	(_comm_resp comm_handle);

// Sending functions
uint8_t	comm_new	(_eMsgAdr eMsgTo, _eMsgAdr eMsgFrom, uint16_t nLength);		/* To, From and Length of the payload */
uint8_t	comm_append	(unsigned char* buffer, unsigned short nLength);			/* Append the payload. call new first */
uint8_t comm_send	(void);														/* Start sending the transmit buffer */
void	comm_response(uint8_t response);										/* Send back a response */

#endif
